Skip to main content

Ansible

Introduction

This cheat sheet provides a quick reference for some common Ansible commands and concepts. Ansible is an open-source automation tool for configuration management, application deployment, and task automation.

Installation

To use Ansible, you need to install it on your control node. Installation methods may vary depending on your operating system. Refer to the official Ansible documentation for installation instructions.

Configuration

Before using Ansible, you should configure your inventory and create Ansible playbooks.

Inventory

The inventory file defines the hosts and groups that Ansible will manage. It is usually named inventory or hosts and can be stored in /etc/ansible/ or your project directory.

Example inventory file (inventory.yml):

web_servers:
hosts:
web1.example.com:
web2.example.com:

db_servers:
hosts:
db1.example.com:
db2.example.com:

# Group vars can be defined for each group
web_servers:
vars:
ansible_ssh_user: ubuntu
ansible_ssh_private_key_file: /path/to/your/private_key.pem

Ansible Playbooks

Playbooks are written in YAML and define a series of tasks to be executed on hosts. Create a playbook file (e.g., my_playbook.yml) to get started.

Example playbook (my_playbook.yml):

---
- name: My Ansible Playbook
hosts: web_servers
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present

- name: Start Nginx service
service:
name: nginx
state: started

Common Ansible Commands

Running Playbooks

  • Run a playbook on all hosts:

    ansible-playbook my_playbook.yml
  • Run a playbook on specific hosts or groups:

    ansible-playbook -i inventory.yml -l web_servers my_playbook.yml

Ad-Hoc Commands

  • Run an ad-hoc command on hosts:
    ansible -i inventory.yml -m <module> -a "<module_arguments>" web_servers

Variables

  • Define variables in playbooks:

    vars:
    my_variable: "some_value"
  • Access variables in playbooks or templates:

    {{ my_variable }}

Conditionals

  • Use conditionals in tasks:

    when: my_variable == "desired_value"

Loops

  • Use loops in tasks:

    with_items:
    - item1
    - item2

Conclusion

This cheat sheet covers some basic Ansible concepts and commands. Ansible provides extensive documentation; refer to the Ansible documentation for more in-depth information and advanced usage.